﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;

namespace AsTekMobile
{
    public static class Cevir
    {
        static CultureInfo provider = new CultureInfo("tr-TR");

        public static T Parse<T>(string sourceValue) where T : IConvertible
        {
            if (string.IsNullOrEmpty(sourceValue))
                return (T)DefaultSetting(typeof(T).ToString());
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T), provider);
            }
            catch
            {
                return (T)DefaultSetting(typeof(T).ToString());
            }
        }

        public static T Parse<T>(object sourceValue) where T : IConvertible
        {
            if (sourceValue == null || sourceValue == DBNull.Value)
                return (T)DefaultSetting(typeof(T).ToString());
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T), provider);
            }
            catch
            {
                return (T)DefaultSetting(typeof(T).ToString());
            }
        }
        public static T Parse<T>(object sourceValue, object defaultValue) where T : IConvertible
        {
            if (sourceValue == null || sourceValue == DBNull.Value)
                return (T)DefaultSetting(typeof(T).ToString());
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T), null);
            }
            catch
            {
                return (T)Convert.ChangeType(defaultValue, typeof(T), null);
            }
        }

        public static T Parse<T>(string sourceValue, string defaultValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T), provider);
            }
            catch
            {
                return (T)Convert.ChangeType(defaultValue, typeof(T), provider);
            }
        }

        public static T Parse<T>(Int16 sourceValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T), provider);
            }
            catch
            {
                return (T)DefaultSetting(typeof(T).ToString());
            }
        }
        public static T Parse<T>(Int16 sourceValue, Int16 defaultValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T), provider);
            }
            catch
            {
                return (T)Convert.ChangeType(defaultValue, typeof(T), provider);
            }
        }

        public static T Parse<T>(Int32 sourceValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T), provider);
            }
            catch
            {
                return (T)DefaultSetting(typeof(T).ToString());
            }
        }
        public static T Parse<T>(Int32 sourceValue, Int32 defaultValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T), provider);
            }
            catch
            {
                return (T)Convert.ChangeType(defaultValue, typeof(T), provider);
            }
        }

        public static T Parse<T>(Int64 sourceValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T), provider);
            }
            catch
            {
                return (T)DefaultSetting(typeof(T).ToString());
            }
        }
        public static T Parse<T>(Int64 sourceValue, Int64 defaultValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T), provider);
            }
            catch
            {
                return (T)Convert.ChangeType(defaultValue, typeof(T), provider);
            }
        }

        public static T Parse<T>(DateTime sourceValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T), provider);
            }
            catch
            {
                return (T)DefaultSetting(typeof(T).ToString());
            }
        }
        public static T Parse<T>(DateTime sourceValue, Int64 defaultValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T), provider);
            }
            catch
            {
                return (T)Convert.ChangeType(defaultValue, typeof(T), provider);
            }
        }

        public static T Parse<T>(Boolean sourceValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T), provider);
            }
            catch
            {
                return (T)DefaultSetting(typeof(T).ToString());
            }
        }
        public static T Parse<T>(Boolean sourceValue, Boolean defaultValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T), provider);
            }
            catch
            {
                return (T)Convert.ChangeType(defaultValue, typeof(T), provider);
            }
        }

        public static T Parse<T>(decimal sourceValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T), provider);
            }
            catch
            {
                return (T)DefaultSetting(typeof(T).ToString());
            }
        }
        public static T Parse<T>(decimal sourceValue, decimal defaultValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T), provider);
            }
            catch
            {
                return (T)Convert.ChangeType(defaultValue, typeof(T), provider);
            }
        }

        public static T Parse<T>(float sourceValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T), provider);
            }
            catch
            {
                return (T)DefaultSetting(typeof(T).ToString());
            }
        }
        public static T Parse<T>(float sourceValue, float defaultValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T), provider);
            }
            catch
            {
                return (T)Convert.ChangeType(defaultValue, typeof(T), provider);
            }
        }

        public static T Parse<T>(byte sourceValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T), provider);
            }
            catch
            {
                return (T)DefaultSetting(typeof(T).ToString());
            }
        }
        public static T Parse<T>(byte sourceValue, byte defaultValue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(sourceValue, typeof(T), provider);
            }
            catch
            {
                return (T)Convert.ChangeType(defaultValue, typeof(T), provider);
            }
        }

        public static object DefaultSetting(string type)
        {
            switch (type)
            {
                case "System.String":
                    return string.Empty;
                case "System.Int32":
                    return 0;
                case "System.Int16":
                    return Convert.ToInt16(0);
                case "System.Int64":
                    return Convert.ToInt64(0);
                case "System.DateTime":
                    return DateTime.Now;
                case "System.Decimal":
                    return Convert.ToDecimal(0);
                case "System.Boolean":
                    return false;
                case "System.Double":
                    return Convert.ToDouble(0);
                case "System.Single":
                    return Convert.ToSingle(0);
                case "System.Byte":
                    return Convert.ToByte(0);
                default:
                    return null;
            }

        }
    }

    public static class Extensions
    {
        public static DateTime ToSmallDate(this DateTime tarih)
        {
            return new DateTime(tarih.Year, tarih.Month, tarih.Day);
        }

        public static int ToInt(this string deger)
        {
            return Cevir.Parse<int>(deger);
        }

        public static string Check(this string deger)
        {
            if (!string.IsNullOrEmpty(deger))
            {
                return deger;
            }
            else
            {
                return "0";
            }
        }
    }
}
